package com.lxmes.business.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.lxmes.base.domain.JcIteminfo;
import com.lxmes.base.domain.JcWarehouseinfoKuwei;
import com.lxmes.base.mapper.JcIteminfoMapper;
import com.lxmes.base.mapper.JcWarehouseinfoKuweiMapper;
import com.lxmes.common.exception.ServiceException;
import com.lxmes.common.utils.DateUtils;
import com.lxmes.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.business.mapper.CcStockinfoInfoMapper;
import com.lxmes.business.domain.CcStockinfoInfo;
import com.lxmes.business.service.ICcStockinfoInfoService;
import org.springframework.transaction.annotation.Transactional;

import static com.lxmes.common.utils.PageUtils.startPage;

/**
 * 库存信息Service业务层处理
 * 
 * @author 张志煜
 * @date 2022-06-12
 */
@Service
public class CcStockinfoInfoServiceImpl implements ICcStockinfoInfoService 
{
    @Autowired
    private CcStockinfoInfoMapper ccStockinfoInfoMapper;

    @Autowired
    private JcIteminfoMapper jcIteminfoMapper;
    @Autowired
    private JcWarehouseinfoKuweiMapper kuweiMapper;

    /**
     * 查询库存信息
     * 
     * @param id 库存信息主键
     * @return 库存信息
     */
    @Override
    public CcStockinfoInfo selectCcStockinfoInfoById(Long id)
    {
        return ccStockinfoInfoMapper.selectCcStockinfoInfoById(id);
    }

    /**
     * 查询库存信息列表
     * 
     * @param ccStockinfoInfo 库存信息
     * @return 库存信息
     */
    @Override
    public List<CcStockinfoInfo> selectCcStockinfoInfoList(CcStockinfoInfo ccStockinfoInfo)
    {

        List<CcStockinfoInfo> resultList = new ArrayList<>();

        List<CcStockinfoInfo> ccStockinfoInfoList = ccStockinfoInfoMapper.selectCcStockinfoInfoList(ccStockinfoInfo);
        for (CcStockinfoInfo stockinfoInfo : ccStockinfoInfoList) {
            //库存可用数量
            BigDecimal useNum = stockinfoInfo.getKcNum().subtract(stockinfoInfo.getLockNum()) ;
            JcIteminfo iteminfo = new JcIteminfo();
            iteminfo.setItemCode(stockinfoInfo.getItemId());
            JcIteminfo iteminfo1 = jcIteminfoMapper.selectJcIteminfo(iteminfo);
            stockinfoInfo.setDownLine(new BigDecimal(iteminfo1.getDownLine()));
            stockinfoInfo.setUpLine(new BigDecimal(iteminfo1.getUpLine()));
            BigDecimal downLine = new BigDecimal(iteminfo1.getDownLine());
            if(useNum.compareTo(downLine)==1){
                //resultList.add(stockinfoInfo);
                stockinfoInfo.setYjflag("库存充足");
            }
            if(useNum.compareTo(downLine)==-1||useNum.compareTo(downLine)==0){
                //resultList.add(stockinfoInfo);
                stockinfoInfo.setYjflag("库存不足");
            }
        }
  /*      if("库存不足".equals(ccStockinfoInfo.getYjflag())){
            ccStockinfoInfoList = ccStockinfoInfoList.stream().filter(a -> a.getYjflag().equals("库存不足")).collect(Collectors.toList());
        }
       if("库存充足".equals(ccStockinfoInfo.getYjflag())){
            ccStockinfoInfoList = ccStockinfoInfoList.stream().filter(a -> a.getYjflag().equals("库存充足")).collect(Collectors.toList());
        }*/
        if("库存不足".equals(ccStockinfoInfo.getYjflag())){
            for (int i =ccStockinfoInfoList.size()-1; i >=0; i--) {
                if("库存不足".equals(ccStockinfoInfoList.get(i).getYjflag())){
                    ccStockinfoInfoList.remove(i);
                }
            }
        }
        if("库存充足".equals(ccStockinfoInfo.getYjflag())){
            for (int i =ccStockinfoInfoList.size()-1; i >=0; i--) {
                if("库存充足".equals(ccStockinfoInfoList.get(i).getYjflag())){
                    ccStockinfoInfoList.remove(i);
                }
            }
        }
        return ccStockinfoInfoList;
    }

    @Override
    public List<CcStockinfoInfo> selectCcStockinfoInfoSelectList(CcStockinfoInfo ccStockinfoInfo) {
        List<CcStockinfoInfo> ccStockinfoInfoList = ccStockinfoInfoMapper.selectCcStockinfoInfoSelectList(ccStockinfoInfo);
        List<CcStockinfoInfo> stockinfoInfos=new ArrayList<>();
        if(StringUtils.isNotEmpty(ccStockinfoInfo.getCodesParams())){
            String[] item =ccStockinfoInfo.getCodesParams();
            String[] lot =ccStockinfoInfo.getLotsParams();
            for (int i = 0; i <item.length ; i++) {
                CcStockinfoInfo stockinfoInfo =new CcStockinfoInfo();
                stockinfoInfo.setItemId(item[i]);
                stockinfoInfo.setLot(lot[i]);
                stockinfoInfos.add(stockinfoInfo);
            }
            ccStockinfoInfoList = ccStockinfoInfoList.stream()
                    .filter(add -> stockinfoInfos.stream()
                            .noneMatch(all -> StringUtils.equals(add.getItemId(), all.getItemId())&&   StringUtils.equals(add.getLot(), all.getLot()) ) ).collect(Collectors.toList());
        }
        return ccStockinfoInfoList;
    }

    @Override
    public List<CcStockinfoInfo> selectCcStockinfoInfoListJc(CcStockinfoInfo ccStockinfoInfo) {
        return  ccStockinfoInfoMapper.selectCcStockinfoInfoListJc(ccStockinfoInfo);
    }

    @Override
    public List<CcStockinfoInfo> selectCcStockinfoInfoListMobile(CcStockinfoInfo ccStockinfoInfo) {
        return  ccStockinfoInfoMapper.selectCcStockinfoInfoListMobile(ccStockinfoInfo);
    }

    @Override
    public List<CcStockinfoInfo> selectCcStockinfoInfoListSum(CcStockinfoInfo ccStockinfoInfo) {

        return ccStockinfoInfoMapper.selectCcStockinfoInfoListSum(ccStockinfoInfo);
    }

    @Override
    public List<CcStockinfoInfo> selectCcStockinfoInfoListinfo(CcStockinfoInfo ccStockinfoInfo) {
        return ccStockinfoInfoMapper.selectCcStockinfoInfoList(ccStockinfoInfo);
    }

    @Override
    public List<CcStockinfoInfo> selectListinfo(CcStockinfoInfo ccStockinfoInfo) {
        return ccStockinfoInfoMapper.selectListinfo(ccStockinfoInfo);
    }


    @Override
    public List<CcStockinfoInfo> selectCcStockinfoInfoListYj(CcStockinfoInfo ccStockinfoInfo) {
        //List<CcStockinfoInfo> ccStockinfoInfoList = ccStockinfoInfoMapper.selectCcStockinfoInfoList(ccStockinfoInfo);
        List<CcStockinfoInfo> ccStockinfoInfoList = ccStockinfoInfoMapper.selectCcStockinfoInfoListSum(ccStockinfoInfo);
        List<CcStockinfoInfo> ccStockinfoInfoListResult = new ArrayList<>();
        for (CcStockinfoInfo stockinfoInfo : ccStockinfoInfoList) {
            JcIteminfo iteminfo = new JcIteminfo();
            iteminfo.setItemCode(stockinfoInfo.getItemId());
            JcIteminfo iteminfo1 = jcIteminfoMapper.selectJcIteminfo(iteminfo);
            if(iteminfo1!=null){
                BigDecimal downLine = new BigDecimal(iteminfo1.getDownLine());
                if(stockinfoInfo.getKcNum().compareTo(downLine)==-1){
                    ccStockinfoInfoListResult.add(stockinfoInfo);
                    stockinfoInfo.setDownLine(new BigDecimal(iteminfo1.getDownLine()));
                    stockinfoInfo.setUpLine(new BigDecimal(iteminfo1.getUpLine()));
                    ccStockinfoInfoListResult.add(stockinfoInfo);
                }
            }
        }
        return ccStockinfoInfoListResult;
    }




    /**
     * 新增库存信息
     * 
     * @param ccStockinfoInfo 库存信息
     * @return 结果
     */
    @Override
    public int insertCcStockinfoInfo(CcStockinfoInfo ccStockinfoInfo)
    {
        ccStockinfoInfo.setCreateTime(DateUtils.getNowDate());
        return ccStockinfoInfoMapper.insertCcStockinfoInfo(ccStockinfoInfo);
    }

    /**
     * 修改库存信息
     * 
     * @param ccStockinfoInfo 库存信息
     * @return 结果
     */
    @Override
    public int updateCcStockinfoInfo(CcStockinfoInfo ccStockinfoInfo)
    {
        ccStockinfoInfo.setUpdateTime(DateUtils.getNowDate());
        return ccStockinfoInfoMapper.updateCcStockinfoInfo(ccStockinfoInfo);
    }

    /**
     * 批量删除库存信息
     * 
     * @param ids 需要删除的库存信息主键
     * @return 结果
     */
    @Override
    public int deleteCcStockinfoInfoByIds(Long[] ids)
    {
        return ccStockinfoInfoMapper.deleteCcStockinfoInfoByIds(ids);
    }

    @Override
    @Transactional
    public int updateStockinfoKuWei(Long[] ids, String kuweiCode) {
        JcWarehouseinfoKuwei kuwei = kuweiMapper.selectJcWarehouseinfoKuweiByKuweiJmCode(kuweiCode);
        if(kuwei==null){
            throw new ServiceException("库位信息不存在！");
        }
        for (int i = 0; i <ids.length ; i++) {
            CcStockinfoInfo ccStockinfoInfo = ccStockinfoInfoMapper.selectCcStockinfoInfoById(ids[i]);
            ccStockinfoInfo.setKuweiCode(kuweiCode);
            ccStockinfoInfo.setKuquName(kuwei.getKuquName());
            ccStockinfoInfo.setKuquCode(kuwei.getKuquCode());
            ccStockinfoInfo.setStockId(kuwei.getStockCode());
            ccStockinfoInfo.setStockName(kuwei.getStockName());
            ccStockinfoInfoMapper.updateCcStockinfoInfo(ccStockinfoInfo);
        }
        return 1;
    }
    /**
     * 删除库存信息信息
     * 
     * @param id 库存信息主键
     * @return 结果
     */
    @Override
    public int deleteCcStockinfoInfoById(Long id)
    {
        return ccStockinfoInfoMapper.deleteCcStockinfoInfoById(id);
    }


}
